//
//  WelcomeViewController.swift
//  Weibo007
//
//  Created by 闫崇 on 15/12/16.
//  Copyright © 2015年 闫崇. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout
class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //从网络上获取用户头像图片
        if let urlString = sharedUserAccount?.avatar_large {
            
            iconView.sd_setImageWithURL(NSURL(string: urlString)!)
            
        }
        
        
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //修改头像底约束的数值
        bottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 200)
        //执行动画
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            //真正执行动画的语句
            self.view.layoutIfNeeded()
            
            }) { _ in
                //头像动画结束后开始执行标签动画
                UIView.animateWithDuration(1.0, animations: {
                    //让标签显示出来
                    self.messageLabel.alpha = 1.0
                    
                    }, completion: { _ in
                        //标签动画结束后发送通知进入 MainTabBarController
                        NSNotificationCenter.defaultCenter().postNotificationName(YCSwitchRootViewController, object: true)
                })
        }
    
    }
    
    
    //头像的底部约束  为了设置动画将底部约束进行改变
    var bottomConstraint : NSLayoutConstraint?
    

    
    override func loadView() {
        
        view = UIView()
        
        let backgroundImage = UIImageView(image: UIImage(named: "ad_background"))
        view.addSubview(backgroundImage)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        //背景图片的自动布局
        backgroundImage.ff_Fill(view)
        
        //头像的自动布局
        //记录 iconView上添加的约束数组
        let cons = iconView.ff_AlignInner(ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -160))
        //在 cons 数组中查找底部约束
        bottomConstraint = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        //标签的自动布局
        messageLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 12))
    }
    
    
    //头像
    lazy var iconView : UIImageView = {
       
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        //设置头像圆角
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        
        
        return iv
        
    }()
    
    //欢迎回来的标签
    lazy var messageLabel : UILabel = {
        
        let label = UILabel()
        label .text = "欢迎回来"
        label.sizeToFit()
        
        //设置标签隐藏  等头像动画显示完成后显示出来
        label.alpha = 0
        
        return label
    }()
    
    

}
